<template>
	<view class="container">
		<!-- 		<view class="left-bottom-sign"></view>
		<view class="back-btn yticon icon-zuojiantou-up" @click="navBack">登录</view>
		<view class="right-top-sign"></view> -->
		<!-- 设置白色背景防止软键盘把下部绝对定位元素顶上来盖住输入框等 -->
		<view class="title">
			<img src="/static/common/logo@2x.png" alt="" class="logo">
			<view class="n">肯天大酒店</view>
		</view>
		<view class="choose-btn">
			<button class="c_btn w" @click="goLogin('wexin')" :disabled="isDisable" open-type="getUserInfo" lang="zh_CN">
				<img src="/static/common/weixin@2x.png" alt="">
				微信登录
			</button>
			<!-- <view class="c_btn" @click="goLogin('phone')">
				手机号一键登录
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		_login,
		_logout
	} from '../../common/vmeitime-http/login.js'
	import {
		_userInfo
	} from '../../common/vmeitime-http/user.js'
	import {
		mapMutations
	} from 'vuex';
	let t = null;
	export default {
		data() {
			return {
				mobile: '',
				password: '',
				logining: false,
				fullInfo: {
					encry: "",
					iv: ""
				},
				isDisable: false
			}
		},
		onLoad() {
			uni.setStorageSync("supertest",0)
		},
		methods: {
			...mapMutations(['login']),
			navBack() {
				uni.navigateBack();
			},
			async toLogin() {
				this.logining = true;
				const {
					mobile,
					password
				} = this;
				/* 数据验证模块
				if(!this.$api.match({
					mobile,
					password
				})){
					this.logining = false;
					return;
				}
				*/
				const sendData = {
					mobile,
					password
				};
				const result = await this.$api.json('userInfo');
				if (result.status === 1) {
					this.login(result.data);
					uni.navigateBack();
				} else {
					this.$api.msg(result.msg);
					this.logining = false;
				}
			},
			async goLogin(n,code) {
				let that = this;
				console.log(n)
				if (n == "wexin") {
					console.log(n)
					uni.removeStorageSync('wxUserInfo');
					this.isDisable = true
					setTimeout(() => {
						this.isDisable = false
					}, 8000);
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes);
							// 获取用户信息
							// that.wxLoginAll(loginRes)
							that.wxLjust(loginRes)
						}
					});
				} else if (n == "phone") {
					uni.navigateTo({
						url: '/pages/public/login_phone'
					});
				} else if ("common") {
					let para = {
						code: code,
						userInfo:uni.getStorageSync('wxUserInfo')
					};
					/*判断是否是退出状态*/
					let authTemp = uni.getStorageSync('authInfo');
					if (authTemp && authTemp !== "") {
						let tempData = await _logout()
						if (tempData.data.code == 200) {
							uni.removeStorageSync('authInfo');
						}
					}
					let wxLogin = await _login(para);
					uni.setStorageSync('authInfo', wxLogin.data.data);
					uni.setStorageSync('userInfo', wxLogin.data.data.userInfo);
																
					
					uni.navigateBack()
					/*判断是否要绑定手机号*/
					// let tempPhone = wxLogin.data.data.userInfo.tel;
					// if (tempPhone && tempPhone !== "") {
					// 	let tempData = await _userInfo();
					// 	if (tempData.data.code == 200) {
					// 		uni.setStorageSync('relUserInfo', tempData.data.data);
					// 		uni.navigateBack();
					// 	}
					// } else {
					// 	uni.redirectTo({
					// 		url: "/pages/user/edit_tel?mark=loginBind"
					// 	})
					// }
				}
			},
			/*判断小程序完全登录状态*/
			wxLoginAll() {
				let that = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						console.log(infoRes)
						uni.setStorageSync('wxUserInfo', infoRes.userInfo);
						that.fullInfo = {
							encry: infoRes.encryptedData,
							iv: infoRes.iv
						}
					},
					fail: function(fRes) {
						console.log(fRes);
					}
				});
			},
			wxLjust(loginRes) {
				let t1 = 0;
				uni.showLoading({
					title: '授权中',
					mask: true
				});
				t = setInterval(() => {
					let tempInfo = uni.getStorageSync('wxUserInfo');
					if (tempInfo && tempInfo !== "") {
						this.goLogin('common', loginRes.code);
						uni.hideLoading()
						clearInterval(t)
					} else {
						t1++;
						if (t1 == 50) {
							uni.showModal({
								title: '授权失败',
								content: '登录功能需要您同意小程序授权才能使用哟，登录后您可以快捷购买商品，还能享受专属客服服务，请重新点击微信登录按钮并在弹出的页面中选择‘允许’，或者您可以选择手机号一键登录。',
								showCancel: false,
								confirmText: "知道啦",
								success: function(res) {
									if (res.confirm) {
										//                                    clearInterval(t)
									} else if (res.cancel) {}
								}
							});
							uni.hideLoading()
							clearInterval(t)
						} else {
							this.wxLoginAll()
						}
					}
				}, 200);
			}

		},

	}
</script>

<style lang='scss'>
	page {
		background: #fff;
	}

	.container {
		/* padding-top: 115px; */
		position: relative;
		width: 100vw;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #fff;
		padding-bottom: 40upx;
	}

	.back-btn {
		position: absolute;
		left: 40upx;
		z-index: 9999;
		padding-top: var(--status-bar-height);
		top: 40upx;
		font-size: 40upx;
		color: $font-color-dark;
	}

	.title {
		text-align: center;
		margin-top: 170upx;
		display: flex;
		flex-direction: column;
		align-items: center;

		img {
			width: 250upx;
			height: 200upx;
			display: inline-block;
			vertical-align: top;
		}

		.n {
			font-size: 50upx;
			color: #282828;
			font-weight: 500;
			display: inline-block;
			margin-top: 15upx;
		}
	}

	.choose-btn {
		margin-top: 137upx;

		.c_btn {
			width: 630upx;
			height: 98upx;
			border-radius: 10upx;
			border: 2upx solid #ffcf31;
			margin: auto;
			color: #ffcf31;
			text-align: center;
			font-size: 30upx;
			line-height: 98upx;

			img {
				display: inline-block;
				width: 49upx;
				height: 42upx;
				vertical-align: sub;
				margin-right: 28upx;
			}
		}

		.w {
			background: #ffcf31;
			color: #ffffff;
			border: none;
			margin-bottom: 39upx;
		}
		button[disabled] {
		    color: #ffffff !important;
		    background: #ffcf31 !important;
		}
	}
</style>
